import 'dart:developer';

import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';

import 'xf_evaluate_platform_interface.dart';

/// An implementation of [XfEvaluatePlatform] that uses method channels.
class MethodChannelXfEvaluate extends XfEvaluatePlatform {
  /// The method channel used to interact with the native platform.
  @visibleForTesting
  final methodChannel = const MethodChannel('xf_evaluate');

  Function(String method, dynamic)? _callback;

  MethodChannelXfEvaluate() {
    initChannelListener();
  }

  void initChannelListener() async {
    methodChannel.setMethodCallHandler((call) async {
      _callback?.call(call.method, call.arguments);
    });
  }

  @override
  Future<String?> getPlatformVersion() async {
    final version =
        await methodChannel.invokeMethod<String>('getPlatformVersion');
    return version;
  }

  @override
  Future<void> destroyVadListener() async {
    await methodChannel.invokeMethod<void>('destroyVadListener');
  }

  @override
  Future<void> setVadListener() async {
    await methodChannel.invokeMethod<void>('setVadListener');
  }

  @override
  Future<void> detectVad(Uint8List frame, int len) async {
    await methodChannel
        .invokeMethod<void>('detect', {"data": frame, "len": len});
  }

  @override
  Future<void> resetVad() async {
    await methodChannel.invokeMethod<void>('resetVad');
  }

  @override
  void setChannelListener(Function(String method, dynamic p1) callback) {
    _callback = callback;
  }

  @override
  Future<void> asyncVadConfig(
      String status_bos_timeout, String status_finish) async {
    await methodChannel.invokeMethod<void>('asyncVadConfig', {
      "status_bos_timeout": status_bos_timeout,
      "status_finish": status_finish
    });
  }
}
